// pages/home/home.ts
import { getPublicPaddingTop } from '../../utils/page'
import { getUserList, getPersonalInformation } from '../../utils/controller'
import {
  $startWuxRefresher,
  $stopWuxRefresher,
  $stopWuxLoader,
} from '../../dist/index'
const getList = (count = 10, step = 0) =>
  [...new Array(count)].map((n, i) => ({
    title: `Pull down ${i + step}`,
    content: 'Wux Weapp',
  }))
Page({
  /**
   * 页面的初始数据
   */
  data: {
    items: [],
    count: 0,
    scrollTop: 0,
    paddingTop: getPublicPaddingTop(),
    userList: [],

    filterPanelVisible: false,

    city: '',
    filterOptions: {
      // attr: null,
      // degree: null,
      // is_cert: null,
      // child_plan: null,
      // is_house: null,
      // is_car: null,
      // annual_income: null,
      // city: null,
    },
    userInfo:{},

    viewStatus: {
      page_no: 1,
      page_size: 10,
    },
  },
  // 弹出vip提示
  goVip(){
    console.log(555);
    
    wx.showModal({
      title: '',
       content: '需要成为会员才能使用高级搜索哦',
       showCancel: true,//是否显示取消按钮
       cancelText:"在想想",//默认是“取消”
      //  cancelColor:'skyblue',//取消文字的颜色
       confirmText:"去升级",//默认是“确定”
       confirmColor: 'skyblue',//确定文字的颜色
       success: res => {
          if (res.cancel) {
             //点击取消,默认隐藏弹框
          } else {
             //点击确定 
            wx.navigateTo({
              url:'/pages/vip/vip'
            })
          }
       },
       fail: function (res) { },//接口调用失败的回调函数 
       
    })
  },
  async getUserInfo(){
    // const personalInfoResult:any = await getPersonalInformation()
    let userInfo = wx.getStorageSync('userInfo')
    
    if(userInfo){
      userInfo = JSON.parse(userInfo)
      this.setData({
        userInfo
      })
    }
    
    
  },
  heighValueChangeAction(e){
    this.setData({
      ['filterOptions.age.max']: e.detail.heighValue
    })
    console.log({e});
    
  },
  lowValueChangeAction(e){
    this.setData({
      ['filterOptions.age.min']: e.detail.lowValue
    })
    console.log({e});
    
  },
  heighValueChangeAction2(e){
    this.setData({
      ['filterOptions.height.max']: e.detail.heighValue
    })
  },
  lowValueChangeAction2(e){
    this.setData({
      ['filterOptions.height.min']: e.detail.lowValue
    })
    
  },
  heighValueChangeAction3(e){
    this.setData({
      ['filterOptions.weight.max']: e.detail.heighValue
    })
  },
  lowValueChangeAction3(e){
    this.setData({
      ['filterOptions.weight.min']: e.detail.lowValue
    })
    
  },
  async getUsersList() {
    const { page_no, page_size } = this.data.viewStatus
    const filterOptions = this.data.filterOptions
    if(filterOptions.age){
      if(filterOptions.age.min && filterOptions.age.max){
        filterOptions.age = filterOptions.age.min + '-' + filterOptions.age.max
      }else{
        filterOptions.age = ''
      }
      
    }
    if(filterOptions.height){
      if(filterOptions.height.min && filterOptions.height.max){
        filterOptions.height = filterOptions.height.min + '-' + filterOptions.height.max
      }else{
        filterOptions.height = ''
      }
      
    }
    if(filterOptions.weight){
      if(filterOptions.weight.min && filterOptions.weight.max){
        filterOptions.weight = filterOptions.weight.min + '-' + filterOptions.weight.max
      }else{
        filterOptions.weight = ''
      }
      
    }
    const city = this.data.city
    filterOptions.city = city
    const result = await getUserList(page_no, page_size, filterOptions)
    console.log('获取用户列表结果', result)

    this.setData({ userList: result })
  },

  bindCityChange(e) {
    this.setData({
      city: e.detail.value[1],
    })
    this.getUsersList()
  },

  showFilterPanel() {
    this.setData({ filterPanelVisible: true })
  },

  hideFilterPanel() {
    this.setData({ filterPanelVisible: false })
  },

  handleFilterGenderChange(e) {
    const sex = +e.currentTarget.dataset.sex
    const filterOptions = this.data.filterOptions
    filterOptions.sex = sex
    this.setData({ filterOptions })
  },

  handleFilterAttrChange(e) {
    const attr = +e.currentTarget.dataset.attr
    const filterOptions = this.data.filterOptions
    filterOptions.attr = attr
    this.setData({ filterOptions })
  },

  handleFilterDegreeChange(e) {
    const degree = +e.currentTarget.dataset.degree
    const filterOptions = this.data.filterOptions
    filterOptions.degree = degree
    this.setData({ filterOptions })
  },

  handleFilterIsCertChange(e) {
    const is_cert = +e.currentTarget.dataset.iscert
    const filterOptions = this.data.filterOptions
    filterOptions.is_cert = is_cert
    this.setData({ filterOptions })
  },

  handleFilterChildPlanChange(e) {
    const child_plan = +e.currentTarget.dataset.childplan
    const filterOptions = this.data.filterOptions
    filterOptions.child_plan = child_plan
    this.setData({ filterOptions })
  },

  handleFilterIsHouseChange(e) {
    const is_house = +e.currentTarget.dataset.ishouse
    const filterOptions = this.data.filterOptions
    filterOptions.is_house = is_house
    this.setData({ filterOptions })
  },

  handleFilterIsCarChange(e) {
    const is_car = +e.currentTarget.dataset.iscar
    const filterOptions = this.data.filterOptions
    filterOptions.is_car = is_car
    this.setData({ filterOptions })
  },

  handleFilterAnnualIncomeChange(e) {
    const annual_income = +e.currentTarget.dataset.income
    const filterOptions = this.data.filterOptions
    filterOptions.annual_income = annual_income
    this.setData({ filterOptions })
  },

  confirmFilter() {
    this.getUsersList()
    this.hideFilterPanel()
  },

  clearFilter() {
    const nativeFilterOptions = {
      // attr: null,
      // degree: null,
      // is_cert: null,
      // child_plan: null,
      // is_house: null,
      // is_car: null,
      // annual_income: null,
      // city: null,
    }
    this.setData({ filterOptions: {} })
  },

  closePanel() {
    this.clearFilter()
    this.hideFilterPanel()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.getUsersList()
    // $startWuxRefresher()
    if (typeof this.getTabBar === 'function') {
      this.getTabBar((tabbar) => {
        tabbar.setData({
          selected: 0,
        })
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    // $stopWuxRefresher()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getUserInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    
    console.log(55);
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
  onPageScroll(e) {
    this.setData({
      scrollTop: e.scrollTop,
    })
  },
  onPulling() {
    console.log('onPulling')
    
  },
  onRefresh() {
    console.log('onRefresh')

    this.setData({ count: 10 })

    setTimeout(() => {
      this.getUsersList()
      $stopWuxRefresher()
    }, 1000)
  },
  onLoadmore() {
    console.log('onLoadmore')
    setTimeout(() => {
      this.setData({
        items: [...this.data.items, ...getList(10, this.data.count)],
        count: this.data.count + 10,
      })

      if (this.data.items.length < 30) {
        $stopWuxLoader()
      } else {
        console.log('没有更多数据')
        $stopWuxLoader('#wux-refresher', this, true)
      }
    }, 3000)
  },
})
